//
//  DealerListViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DealerListViewController.h"
#import "UIAlertView+Utilities.h"
#import "DealerAddressViewController.h"
#import "ZHWebService.h"
#import "ZHDataService.h"
#import "WDDealer.h"
#import "WDProvince.h"
#import "HeaderView.h"

@implementation DealerListViewController
@synthesize myTableView;
@synthesize currentProvince, latitude, longtitude;
@synthesize allDealers;

#pragma mark - my own methods
- (void)getAllDealers
{
    CompletionBlock completion = ^(NSData *data){
        NSArray *dealers = [ZHDataService parseToAllDealersWithData:data];
        self.allDealers = dealers;
        [myTableView reloadData];
    };
    ErrorBlock error = ^(NSString *error){};
    [[ZHWebService sharedService] getAllDealersWithProvinceId:self.currentProvince.provinceId lat:self.latitude lon:self.longtitude onCompletion:completion onError:error];
    
}

#pragma mark - View lifecycle
- (void)dealloc
{
    [allDealers release];
    [myTableView release];
    [currentProvince release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"经销商列表";
    [self setBackgroundImageViewForTabView];
    [self getAllDealers];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.myTableView = nil;
}

#pragma -
#pragma mark - UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.allDealers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify";
    DealerListTableCell *cell = (DealerListTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if(cell == nil)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"DealerListTableCell" owner:self options:nil];
        cell = (DealerListTableCell *)[nibs lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
    }
    WDDealer *dealer = [self.allDealers objectAtIndex:indexPath.row];
    cell.dealerNameLabel.text = dealer.name;
    cell.dealerPhoneLabel.text = dealer.telephone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDealerListTableCellHeight;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeaderViewHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
    HeaderView *hv = (HeaderView *)[nibs lastObject];
    hv.titleLabel.text = [NSString stringWithFormat:@"共%d经销商",[self.allDealers count]];
    return hv;
}

#pragma mark - DealerListTableCellDelegate methods
- (void)didCallingDealerNum:(DealerListTableCell *)cell
{
    UIAlertView *alertView  = [[UIAlertView alloc] initWithTitle:cell.dealerPhoneLabel.text message:@"是否拨叫该号码？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
}

- (void)didShowingAddressInMapForDealer:(DealerListTableCell *)cell
{
    NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
    WDDealer *dealer = [self.allDealers objectAtIndex:indexPath.row];
    DealerAddressViewController *dealerAddressController = [[DealerAddressViewController alloc] init];
    dealerAddressController.dealer = dealer;
    [self.navigationController pushViewController:dealerAddressController animated:YES];
    [dealerAddressController release];
}
#pragma mark - UIAlertViewDelegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",alertView.title]]];
}

@end
